
Imports cmplib01
Imports System.Drawing


Public Module deploymentZoneCaptureModule

    Private Const c_Module As String = "deploymentZoneCaptureModule"

    Public Enum presence_en
        friendly = 0
        enemy = 1
    End Enum

    Public Sub checkDeploymentZoneCaptures(ByRef game As cmplib01.CampaignerGameData)
        '// methodology
        ' run through each DEPLOYMENT ZONE
        ' test to see if any chits from the opposing faction are within it's bounds AT THE END OF THE TURN
        '   check capture event if enemy present
        Try
            Dim forcePresence(1) As Boolean '// 0 = friendly, 1 = enemy (can be expanded in the future to actual FACTION #.....

            For Each key As String In game.DeploymentZones_g.HT.Keys
                Dim zone As DeploymentZone = game.DeploymentZones_g.Item(key)

                If zone.Capturable = True Then

                    '// reset contested flag
                    zone.Contested = False

                    '// reset forcepresence
                    forcePresence(0) = False
                    forcePresence(1) = False

                    '// see if there are any enemy and/or friendly in this zone.
                    For Each chit As ChitMember In game.Chits
                        If chit.OwningFaction.FactionID.Equals(zone.FactionID) Then
                            forcePresence(0) = IsChitWithin(zone, chit)
                        Else
                            forcePresence(1) = IsChitWithin(zone, chit)
                        End If
                    Next

                    '//  if forcepresence(0) andalso forcepresence(1) then contested
                    If (forcePresence(0) = True) And (forcePresence(1) = True) Then zone.Contested = True

                    '// if there is no enemy, we can unmark the contested and captured flags
                    If forcePresence(1) = False Then
                        zone.Contested = False
                        zone.Captured = False
                    End If

                    '// if forcepresence(0)=false andalso forcepresence(1)=true, then captured
                    '// if already captured, then determine ownership change or hold.
                    If (forcePresence(0) = False) And (forcePresence(1) = True) Then HandleCaptureEvent(zone, game)
                End If

            Next

        Catch ex As Exception
            HandleException(c_Module & ".CheckDeploymentZoneCaptures", ex)
        End Try

    End Sub

    Public Function IsChitWithin(ByRef dp As DeploymentZone, ByRef chit As ChitMember) As Boolean
        Try
            '// checks the preciselocation of the provided chit to determine if it is currently within the provided deployment zone
            Dim result As Boolean = False

            Dim reg As New Region(dp.BoundaryGraphicsPath)
            If reg.IsVisible(chit.PreciseLocation.X, chit.PreciseLocation.Y) Then
                result = True
            End If

            Return result

        Catch ex As Exception
            HandleException(c_Module & ".IsChitTresspassing", ex)
            Return False
        End Try
    End Function

    Friend Sub HandleCaptureEvent(ByRef zone As DeploymentZone, ByRef game As cmplib01.CampaignerGameData)
        '// we're here because 
        Try
            '// we are already captured
            If zone.Captured Then
                '// do whatever the capture says to doooooo
                Select Case zone.CaptureEvent
                    Case CaptureEvent_cgd.Hold
                        '// nothing special happens here, as long as a chit is maintained, the zone remains captured.
                        zone.Captured = True '/// we stay captured... :P

                    Case CaptureEvent_cgd.Destroyed
                        zone.Active = False '// set it to inactive!

                    Case CaptureEvent_cgd.ChangeOwnership
                        FlipDeploymentZoneOwnership(zone, game)

                End Select
            Else
                '// this zone was not already captured
                zone.Captured = True
            End If
        Catch ex As Exception
            HandleException(c_Module & ".HandleCaptureEvent", ex)
        End Try
    End Sub

    Friend Sub FlipDeploymentZoneOwnership(ByRef zone As DeploymentZone, ByRef game As CampaignerGameData)
        Try
            Dim factID(1) As Short
            Dim i As Integer

            For Each key As String In game.Faction_g.HT.Keys
                factID(i) = game.Faction_g.Item(key).FactionID
                i += 1
            Next

            zone.Captured = False
            zone.Contested = False

#If DEBUG Then
            Debug.WriteLine("flipping ownership of deployment zone " & zone.ZoneName)
#End If

            If zone.FactionID.Equals(factID(0)) Then
                zone.FactionID = factID(1)
            Else
                zone.FactionID = factID(0)
            End If

        Catch ex As Exception
            HandleException(c_Module & ".FlipDeploymentZoneOwnership", ex)
        End Try
    End Sub

End Module
